#include "vfs.h"

namespace SkyID 
{

//- VFS -----------------------------------------------------------------------

namespace VFS
{

static t_package_array gPackages;

bool initialize(const Config &config)
{
	gPackages.reserve(10);

	LOG_MSG("VFS: initialized");
	return true;
}

void shutdown()
{
	t_package_iterator i = gPackages.begin(), ie = gPackages.end();
	while (i != ie)
		delete[] *i++;
	gPackages.clear();
	LOG_MSG("VFS: shutdown");
}

bool addPackage(const char *fileName)
{
	size_t length = strlen(fileName);
	char *buffer = new char[length+1];
	strcpy(buffer, fileName);
	gPackages.push_back(buffer);
	return true;
}

void removeAllPackages()
{
	gPackages.clear();
}

Reader* getReader(const char *fileName)
{
	Reader *reader = NULL;
	reader = new ReaderFile(fileName);
	LOG_ASSERT(reader);
	if (reader->isValid())
		return reader;
	delete reader;
	reader = NULL;
	t_package_iterator i = gPackages.begin(), ie = gPackages.end();
	while (i != ie)
	{
		reader = new ReaderZip(*i++, fileName);
		LOG_ASSERT(reader);
		if (reader->isValid())
			break;
		delete reader;
		reader = NULL;
	}
	if (!reader)
		LOG_MSG("VFS: cant find file '%s'", fileName);
	return reader;
}

} // namespace VFS

} // namespace SkyID
